package com.example.zyfypt_309.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;

import com.example.zyfypt_309.R;
import com.example.zyfypt_309.bean.MyApplication;
import com.example.zyfypt_309.model.LogoutModel;

import butterknife.ButterKnife;
import butterknife.OnClick;

public class SettingActivity extends AppCompatActivity {


    private String sessionid;
    private MyApplication app;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_setting);
        ButterKnife.bind(this);
        app = (MyApplication) getApplication();
        sessionid = app.getSessionid();
        toolbarInit();
    }

    private void toolbarInit() {
        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle(R.string.action_settings);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @OnClick(R.id.logout_btn)
    public void logout() {
        LogoutModel logoutModel = new LogoutModel();
        //如果注销成功
        if (logoutModel.logout(sessionid)) {
            sessionid = null;
            app.setSessionid(null);
            Intent intent = new Intent(this, LoginActivity.class);
            startActivity(intent);
        }
    }
}
